
skoolCare
        .directive('slimScroll', function($timeout) {
            return {
                restrict: 'A',
                link: function(scope, el, attrs) {

                    $timeout(function() {

                        $(el[0]).slimScroll();

                    }, 100);
                }
            };
        })
        .directive('ngConfirmClick', [
            function() {
                return {
                    link: function(scope, element, attr) {
                        var msg = attr.ngConfirmClick || "Are you sure want to delete?";
                        var clickAction = attr.confirmedClick;

                        element.bind('click', function(event) {
                            if (window.confirm(msg)) {
                                console.log('Deleted..');
                                //scope.$eval(clickAction);
                                scope.$apply(clickAction);
                            }
                            else {
                                console.log('Not Deleted..');
                            }
                        });
                    }
                };
            }])

// Confirm Password Validation
        .directive('validConfirmPassword', function() {
            return {
                require: 'ngModel',
                link: function(scope, elm, attrs, ctrl) {
                    ctrl.$parsers.unshift(function(viewValue, $scope) {
                        var noMatch = viewValue != scope.myForm.password.$viewValue
                        ctrl.$setValidity('noMatch', !noMatch)
                    })
                }
            }
        })

        // Loader
        .directive('loading', function() {
            return {
                restrict: 'E',
                replace: true,
                template: '<div class="loading">LOADING...</div>',
                link: function(scope, element, attr) {
                    scope.$watch('loading', function(val) {
                        if (val)
                            $(element).show();
                        else
                            $(element).hide();
                    });
                }
            }
        })
// Only Number

//        .directive('onlynum', function() {
//            return {
//                require: 'ngModel',
//                link: function(scope) {
//                    scope.$watch('chapter.chapterNo', function(newValue, oldValue) {
//                        var arr = String(newValue).split("");
//                        if (arr.length === 0)
//                            return;
//                        if (arr.length === 1 && (arr[0] == '-' || arr[0] === '.'))
//                            return;
//                        if (arr.length === 2 && newValue === '-.')
//                            return;
//                        if (isNaN(newValue)) {
//                            scope.chapter.chapterNo = oldValue;
//                        }
//                    });
//                }
//            };
//        })
        .directive('onlynum', function() {

            return {
                require: 'ngModel',
                link: function(scope, element, attr, ngModelCtrl) {

                    function fromUser(text) {

                        var transformedInput = text.replace(/[^0-9]/g, '');

                        console.log(transformedInput);

                        if (transformedInput !== text) {

                            ngModelCtrl.$setViewValue(transformedInput);

                            ngModelCtrl.$render();

                        }

                        return transformedInput;

                    }

                    ngModelCtrl.$parsers.push(fromUser);

                }

            };

        })

        .directive('pulsate', function($timeout) {
            return {
                restrict: 'A',
                scope: {
                    color: "@"
                },
                link: function(scope, el, attrs) {

                    $(el[0]).pulsate({
                        color: scope.color,
                        reach: 20,
                        speed: 1000,
                        pause: 0,
                        glow: true,
                        repeat: true,
                        onHover: false
                    });
                }
            }
        })

        .directive('timer', ['$compile', function($compile) {
                return  {
                    restrict: 'EAC',
                    replace: false,
                    scope: {
                        interval: '=interval',
                        startTimeAttr: '=startTime',
                        endTimeAttr: '=endTime',
                        countdownattr: '=countdown',
                        autoStart: '&autoStart',
                        maxTimeUnit: '='
                    },
                    controller: ['$scope', '$element', '$attrs', '$timeout', function($scope, $element, $attrs, $timeout) {

                            // Checking for trim function since IE8 doesn't have it
                            // If not a function, create tirm with RegEx to mimic native trim
                            if (typeof String.prototype.trim !== 'function') {
                                String.prototype.trim = function() {
                                    return this.replace(/^\s+|\s+$/g, '');
                                };
                            }

                            //angular 1.2 doesn't support attributes ending in "-start", so we're
                            //supporting both "autostart" and "auto-start" as a solution for
                            //backward and forward compatibility.
                            $scope.autoStart = $attrs.autoStart || $attrs.autostart;

                            if ($element.html().trim().length === 0) {
                                $element.append($compile('<span>{{millis}}</span>')($scope));
                            } else {
                                $element.append($compile($element.contents())($scope));
                            }

                            $scope.startTime = null;
                            $scope.endTime = null;
                            $scope.timeoutId = null;
                            $scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) >= 0 ? parseInt($scope.countdownattr, 10) : undefined;
                            $scope.isRunning = false;

                            $scope.$on('timer-start', function() {
                                $scope.start();
                            });

                            $scope.$on('timer-resume', function() {
                                $scope.resume();
                            });

                            $scope.$on('timer-stop', function() {
                                $scope.stop();
                            });

                            $scope.$on('timer-clear', function() {
                                $scope.clear();
                            });

                            $scope.$on('timer-set-countdown', function(e, countdown) {
                                $scope.countdown = countdown;
                            });

                            function resetTimeout() {
                                if ($scope.timeoutId) {
                                    clearTimeout($scope.timeoutId);
                                }
                            }

                            $scope.start = $element[0].start = function() {
                                $scope.startTime = $scope.startTimeAttr ? new Date($scope.startTimeAttr) : new Date();
                                $scope.endTime = $scope.endTimeAttr ? new Date($scope.endTimeAttr) : null;
                                if (!$scope.countdown) {
                                    $scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
                                }
                                resetTimeout();
                                tick();
                                $scope.isRunning = true;
                            };

                            $scope.resume = $element[0].resume = function() {
                                resetTimeout();
                                if ($scope.countdownattr) {
                                    $scope.countdown += 1;
                                }
                                $scope.startTime = new Date() - ($scope.stoppedTime - $scope.startTime);
                                tick();
                                $scope.isRunning = true;
                            };

                            $scope.stop = $scope.pause = $element[0].stop = $element[0].pause = function() {
                                $scope.clear();
                                $scope.$emit('timer-stopped', {millis: $scope.millis, seconds: $scope.seconds, minutes: $scope.minutes, hours: $scope.hours, days: $scope.days});
                            };

                            $scope.clear = $element[0].clear = function() {
                                // same as stop but without the event being triggered
                                $scope.stoppedTime = new Date();
                                resetTimeout();
                                $scope.timeoutId = null;
                                $scope.isRunning = false;
                            };

                            $element.bind('$destroy', function() {
                                resetTimeout();
                                $scope.isRunning = false;
                            });

                            function calculateTimeUnits() {

                                // compute time values based on maxTimeUnit specification
                                if (!$scope.maxTimeUnit) {
                                    $scope.seconds = Math.floor(($scope.millis / 1000) % 60);
                                    $scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
                                    $scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
                                    $scope.days = Math.floor((($scope.millis / (3600000)) / 24));
                                } else if ($scope.maxTimeUnit === 'second') {
                                    $scope.seconds = Math.floor($scope.millis / 1000);
                                    $scope.minutes = 0;
                                    $scope.hours = 0;
                                    $scope.days = 0;
                                } else if ($scope.maxTimeUnit === 'minute') {
                                    $scope.seconds = Math.floor(($scope.millis / 1000) % 60);
                                    $scope.minutes = Math.floor($scope.millis / 60000);
                                    $scope.hours = 0;
                                    $scope.days = 0;
                                } else if ($scope.maxTimeUnit === 'hour') {
                                    $scope.seconds = Math.floor(($scope.millis / 1000) % 60);
                                    $scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
                                    $scope.hours = Math.floor($scope.millis / 3600000);
                                    $scope.days = 0;
                                }

                                // plural - singular unit decision
                                $scope.secondsS = $scope.seconds == 1 ? '' : 's';
                                $scope.minutesS = $scope.minutes == 1 ? '' : 's';
                                $scope.hoursS = $scope.hours == 1 ? '' : 's';
                                $scope.daysS = $scope.days == 1 ? '' : 's';
                                //add leading zero if number is smaller than 10
                                $scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
                                $scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
                                $scope.hhours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
                                $scope.ddays = $scope.days < 10 ? '0' + $scope.days : $scope.days;

                            }
                            //determine initial values of time units and add AddSeconds functionality
                            if ($scope.countdownattr) {
                                $scope.millis = $scope.countdownattr * 1000;

                                $scope.addCDSeconds = $element[0].addCDSeconds = function(extraSeconds) {
                                    $scope.countdown += extraSeconds;
                                    $scope.$digest();
                                    if (!$scope.isRunning) {
                                        $scope.start();
                                    }
                                };

                                $scope.$on('timer-add-cd-seconds', function(e, extraSeconds) {
                                    $timeout(function() {
                                        $scope.addCDSeconds(extraSeconds);
                                    });
                                });
                            } else {
                                $scope.millis = 0;
                            }
                            calculateTimeUnits();

                            var tick = function() {

                                $scope.millis = new Date() - $scope.startTime;
                                var adjustment = $scope.millis % 1000;

                                if ($scope.endTimeAttr) {
                                    $scope.millis = $scope.endTime - new Date();
                                    adjustment = $scope.interval - $scope.millis % 1000;
                                }


                                if ($scope.countdownattr) {
                                    $scope.millis = $scope.countdown * 1000;
                                }

                                if ($scope.millis < 0) {
                                    $scope.stop();
                                    $scope.millis = 0;
                                    calculateTimeUnits();
                                    return;
                                }
                                calculateTimeUnits();

                                //We are not using $timeout for a reason. Please read here - https://github.com/siddii/angular-timer/pull/5
                                $scope.timeoutId = setTimeout(function() {
                                    tick();
                                    $scope.$digest();
                                }, $scope.interval - adjustment);

                                $scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis});

                                if ($scope.countdown > 0) {
                                    $scope.countdown--;
                                }
                                else if ($scope.countdown <= 0) {
                                    $scope.stop();
                                }
                            };

                            if ($scope.autoStart === undefined || $scope.autoStart === true) {
                                $scope.start();
                            }
                        }]
                };
            }]);

